<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<%
			String path = request.getContextPath();
			//String fbh = request.getParameter("fbh");
		%>
		<title>报表操作</title>
		<link rel="stylesheet" type="text/css"
			href="<%=path%>/css/btn_class.css" />
		<link rel="stylesheet" type="text/css"
			href="<%=path%>/extjs/resources/css/ext-all.css" />
		<link rel="stylesheet" type="text/css"
			href="<%=path%>/extjs/style.css" />
		<script type="text/javascript"
			src="<%=path%>/extjs/adapter/ext/ext-base.js"></script>
		<script type="text/javascript" src="<%=path%>/extjs/ext-all.js"></script>
<script type="text/javascript"	src="<%=path%>/js/common.js"></script>
<script type="text/javascript"	src="<%=path%>/js/produceReportHead.js"></script>
<script type="text/javascript"	src="<%=path%>/js/opReport.js"></script>
<script type="text/javascript" src="<%=path%>/menu/xtsz/table_setting.js"></script>

		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	</head>
	<body>
		<div id="west-div"
			style="width: 280; height: 400px; border: 1px solid #c3daf9;"></div>
		<div id="center-div"></div>
	</body>
	<script type="text/javascript">
	
	Ext.BLANK_IMAGE_URL = "<%=path%>/extjs/resources/images/default/s.gif";
	
	Ext.onReady(function() {
		
		Ext.QuickTips.init();
		var fbh_table = ""; //当前报表编号
		// shorthand
		var Tree_op = Ext.tree;
		var loader_op = new Tree_op.TreeLoader({
			dataUrl : 'reportInfo!findReportChartTree.do'
		});
		loader_op.on("beforeload", function(treeLoader, node) {
			loader_op.baseParams.fflag = "1";//报表类型树
			//loader.baseParams.fcode  = node.attributes.fcode;
		}, this);

		loader_op.processResponse = function(response, node, callback) {
			var json1 = response.responseText;
			try {
				var json = eval("(" + json1 + ")");
				node.beginUpdate();
				//从json中取得json数组
				var o = json["key"];
				for ( var i = 0, len = o.length; i < len; i++) {
					var n = this.createNode(o[i]);
					if (n) {
						node.appendChild(n);
					}
				}
				node.endUpdate();
				if (typeof callback == "function") {
					callback(this, node);
				}
			} catch (e) {
				this.handleFailure(response);
			}
		};

		var tree_op = new Tree_op.TreePanel({
			region : 'center',
			id : 'west-panel',
			el : 'west-div',
			width : 280,
			//	title : '所属区县',
			useArrows : true,
			autoScroll : true,
			animate : true,
			enableDD : true,
			containerScroll : true,
			rootVisible : false, //树根不可见
			loader : loader_op
		});

		//单击事件
		tree_op.on('click', function(node) {
			//alert(node.attributes.ftype);
			var sqlStr = node.attributes.fsql;
			sqlStr = sqlStr.replace(/\@/g,"'");//将所有@替换为'
			Ext.get("fsql_id").dom.value = sqlStr;
			Ext.getCmp("ccgc_radio_id").setValue(node.attributes.fprocedure_open);
			var produceStr = node.attributes.fprocedure_name;
			if(produceStr == null || produceStr == "null") {
				produceStr = "";
			}
			Ext.get("fprocedure_name_id").dom.value = produceStr;
			fbh_table = node.id;
			Ext.get("fbh_id_form").dom.value = node.id;
			Ext.get("fbh_id").dom.value = node.id;
			Ext.get("fname_id").dom.value = node.text;
			Ext.get("ftype_id").dom.value = node.attributes.ftype;
			if (node.isLeaf()) { //是报表
				view_ds.removeAll();
				view_ds.load({
					params : {
						'fbh' : node.id
					}
				});
			}
		}, tree_op);

		/*
		//checkBox勾选事件
		tree.on('checkchange', function(node, checked) {
			node.expand();
			node.attributes.checked = checked;
			node.eachChild(function(child) {
				child.ui.toggleCheck(checked);
				child.attributes.checked = checked;
				child.fireEvent('checkchange', child, checked);
			});
		}, tree);

		 */
		// set the root node
		var root_op = new Tree_op.AsyncTreeNode({
			text : '所有报表',
			draggable : false,
			id : '0',
			fbh : '-1',
			ftype : '1'
		});
		tree_op.setRootNode(root_op);

		/**********************************   以下是报表操作方法   **************************************************************/
		var btn_add_table = new Ext.Button({
			text : '新增',
			iconCls : 'sy-add',
			handler : function() {
				var fbhStr = Ext.get("fbh_id").dom.value;
				var fnameStr = Ext.get("fname_id").dom.value;
				var ftypeStr = Ext.get("ftype_id").dom.value;
				
				if (ftypeStr == '1') {
					top.Ext.MessageBox.alert('提示', '不能针对报表进行新增操作！');
				} else {
					if (fbhStr) {
						add_table_fun(fbhStr, fnameStr, tree_op); //引自setting.js
					} else {
						top.Ext.MessageBox.alert('提示', '请选择下面节点再进行新增操作！');
					}
				}
			}
		});
		
		var btn_set_table = new Ext.Button({
			text : '设计',
			iconCls : 'sy-add',
			handler : function() {
				var fbhStr = Ext.get("fbh_id").dom.value;
				var fnameStr = Ext.get("fname_id").dom.value;
				var ftypeStr = Ext.get("ftype_id").dom.value;
				
				if (ftypeStr == '1') {
					if (fbhStr) {
						design_table_fun(fbhStr, fnameStr, tree_op); //引自setting.js
					} else {
						top.Ext.MessageBox.alert('提示', '请选择报表进行设计操作！');
					}
				} else {
					top.Ext.MessageBox.alert('提示', '只能针对报表进行设计操作！');
				}
			}
		});

		var btn_update_table = new Ext.Button({
			text : '修改',
			iconCls : 'sy-modify',
			handler : function() {
				var fbhStr = Ext.get("fbh_id").dom.value;
				var fnameStr = Ext.get("fname_id").dom.value;
				var ftypeStr = Ext.get("ftype_id").dom.value;
				if (ftypeStr == '1') {
					mod_table_fun(fbhStr, fnameStr, tree_op); //引自setting.js
				} else {
					top.Ext.MessageBox.alert('提示', '只能针对报表进行修改！');
				}
			}
		});

		var btn_delete_table = new Ext.Button({
			text : '删除',
			iconCls : 'sy-delete',
			handler : function() {
				var fbhStr = Ext.get("fbh_id").dom.value;
				var ftypeStr = Ext.get("ftype_id").dom.value;
				if (ftypeStr == '1') {
					top.Ext.Msg.confirm('确认删除', '您确定删除所选报表吗,删除后将无法恢复？', function(btn) {
						if (btn == 'yes') {
							Ext.Ajax.request({
								url : 'reportInfo!deleteReportInfo.do',
								params : {
									"fbh" : fbhStr,
									"ftype" : "1"
								},
								success : function() {
									tree_op.root.reload();
									top.Ext.Msg.alert('', '删除报表成功！');
								},
								failure : function() {
									Ext.Msg.show({
										title : '错误提示',
										msg : '删除报表失败！',
										buttons : Ext.Msg.OK,
										icon : Ext.Msg.ERROR
									});
								}
							});
						}
					});
				} else {
					top.Ext.Msg.confirm('确认删除', '删除报表类别会导致该类别下面所有报表不可用，确定要删除吗？', function(btn) {
						if (btn == 'yes') {
							Ext.Ajax.request({
								url : 'reportInfo!deleteReportInfo.do',
								params : {
									"fbh" : fbhStr,
									"ftype" : "2"
								},
								success : function() {
									tree_op.root.reload();
									top.Ext.Msg.alert('', '删除报表类别成功！');
								},
								failure : function() {
									top.Ext.Msg.alert('', '删除报表类别失败！');
								}
							});
						}
					});
				}
			}
		});
		
		var tree_nav = new Ext.Panel({
			//title : '信息',
			region : 'west',
			split : true,
			layoutConfig:{
	            animate:true
	        },
			width : 290,
			layout : 'fit',
			autoScroll : true,
			collapsible : true,
			tbar : [ btn_add_table, '-', btn_set_table, '-', btn_update_table, '-',
					btn_delete_table, {
						xtype : 'hidden',
						id : 'fbh_id',
						name : 'fbh'
					}, {
						xtype : 'hidden',
						id : 'fname_id',
						name : 'fname'
					}, {
						xtype : 'hidden',
						id : 'ftype_id',
						name : 'ftype'
					} ],
			items : [ tree_op ]
		});
		
		
		var titleStr = top.Ext.getCmp('center-panel').getActiveTab().title; //panel  title
		var strs = new Array(); //定义一数组
		strs = titleStr.split("-"); //字符分割      
		
		var parameter_Jthtwb = Ext.data.Record.create([ {
			name : 'fid',
			mapping : 'fid',
			type : 'string'
		},{
			name : 'fbh',
			mapping : 'fbh',
			type : 'string'
		},{
			name : 'fname',
			mapping : 'fname',
			type : 'string'
		},{
			name : 'fdesc',
			mapping : 'fdesc',
			type : 'string'
		},{
			name : 'ftype',
			mapping : 'ftype',
			type : 'string'
		}]);
		
		var view_ds = new Ext.data.Store({
			reader : new Ext.data.JsonReader({
				totalProperty : 'totalProperty',
				root : 'root'
			}, parameter_Jthtwb),
			idProperty : 'threadid',
			remoteSort : true,
			url: 'reportInfo!queryParameterInfo.do'
		});
		
		//定义勾选框，不需要可不必定义
		var view_sm = new Ext.grid.CheckboxSelectionModel({
			singleSelect : false //单选
		});

		//参数类型
		var fcolType_store = new Ext.data.SimpleStore( {
				fields : [ 'ftype', 'ftype_desc' ],
				data : [ [ 1, '文本' ], [ 2, '数字' ], [ 3, '日期' ] ]});
		
		//定义列
		var view_colM = new Ext.grid.ColumnModel([ view_sm, new Ext.grid.RowNumberer(),//增加自动编号，不需要可不必定义
		{
			header : '参数名',
			dataIndex : 'fname',
			width : 100,
			renderer : function(value, metadata, record, rowIndex, colIndex, store) {
            	if(value == null || value == "null") {
            		value = "";
            	} else {
            		return value;
            	}
            	
            } ,
			editor: new Ext.form.TextField({
               allowBlank: true,
               style: 'text-align:left'
            }),
			sortable : true
		},{
            header: "参数类型",
            width: 80,
            sortable: true,
            dataIndex: 'ftype',
            renderer : function(value, cellmeta, record){
            	//获取当前id="combo_process"的comboBox选择的值
				var index = fcolType_store.find(Ext.getCmp('combo_type').valueField, value);
				var record = fcolType_store.getAt(index);
				var displayText = "";
				// 如果下拉列表没有被选择，那么record也就不存在，这时候，返回传进来的value，
				// 而这个value就是grid的deal_with_name列的value，所以直接返回
				if (record == null) {
					//返回默认值，这是与网上其他解决办法不同的。这个方法才是正确的。我研究了很久才发现。
					displayText = value;
				} else {
					displayText = record.data.ftype;//获取record中的数据集中的process_name字段的值
				}
				
				if(displayText == null || displayText == "null") {
					return "";
				} else {
					return displayText;
				}
				
            },
            editor: new Ext.form.ComboBox({
               //typeAhead: true,
               mode : 'local',
                    hiddenName : 'ftype',
					hiddenValue : 'ftype',
					valueField : 'ftype',
					displayField : 'ftype_desc',
					triggerAction : 'all',
					id :"combo_type",
              		// transform:'light',
               		store : fcolType_store,
               		listeners : {
						select : function(obj) { 
							var record = rctx_grid.getSelectionModel().getSelected();
							//个性化定制信息内容
							if(obj.value == 3) {
							}
						}
					}
               //listClass: 'x-combo-list-small'
            })
        }, {
			header : '备注',
			dataIndex : 'fdesc',
			width : 400,
			renderer : function(value, metadata, record, rowIndex, colIndex, store) {
				if(value == null || value == "null") {
            		value = "";
            	} else {
            		return value;
            	}
            } ,
			editor: new Ext.form.TextField({
               allowBlank: true,
               style: 'text-align:left'
            }),
			sortable : true
		} ]);
		
		
		
		var btn_add_parameter = new Ext.Button({
				text : '新增',
				iconCls : 'sy-add',
				handler : function() {
					var myDate = new Date();  // 得到系统日期 
					myDate.getFullYear();  //获取完整的年份(4位,1970-????) 
					myDate.getMonth();      //获取当前月份(0-11,0代表1月) 
					myDate.getDate();      //获取当前日(1-31) 
					myDate.getHours();      //获取当前小时数(0-23) 
					myDate.getMinutes();    //获取当前分钟数(0-59) 
					myDate.getSeconds();    //获取当前秒数(0-59) 
					myDate.getMilliseconds();  //获取当前毫秒数(0-999) 
					 
					var random_str = ("" + Math.random()).substring(2,8);
					
					var f_id = myDate.getFullYear() + "" + myDate.getMonth() 
							   + myDate.getDate() + myDate.getHours() 
							   + myDate.getMinutes() + myDate.getSeconds()
							   + myDate.getMilliseconds() + random_str;
							   
	                var p = new parameter_Jthtwb({
	                    fid : f_id,
	                    fbh : Ext.get("fbh_id").dom.value,
	        	 		fname : '',
	        	 		fdesc : '',
	        	 		ftype : 1
	                });
	                view_grid.stopEditing();
	                view_ds.insert(0, p);
	                view_grid.startEditing(0, 0);
				}
			});
		
		
	var btn_delete_parameter = new Ext.Button({
			text : '删除',
			iconCls : 'sy-delete',
			handler : function(btn) {
				Ext.Msg.confirm('确认删除', '您确定删除所选记录吗？', function(btn) {
					if (btn == 'yes') {
						
						var view_record = view_grid.getSelectionModel().getSelected();
						if (view_record) {
							var array = view_sm.getSelections();
							var idData = ""; 
							Ext.each(array, function(view_record) {
								idData += "'" + view_record.get("fid") + "',";
							});
							
							if (idData.length == 0) {
								idData = ""; 
							} else {
								idData = idData.substring(0, idData.length - 1);
							}
							//提交
							Ext.Ajax.request({
								url : 'reportInfo!deleteReportParameters.do',// ?id='+uid,
								method : 'POST',
								params : {
									"idData" : idData
								},
								success : function(response, action) {
									view_ds.removeAll();
									view_ds.reload();
								},
								// 提交失败的回调函数
								failure : function() {
									Ext.Msg.alert('错误', '服务器出现错误请稍后再试！');
								}
							});
						}
					} else {
						Ext.MessageBox.alert('提示', '请选择一行或多行纪录进行操作！');
						return;
					}
				});
			}
		});
	
	var btn_refresh_parameter = new Ext.Button({
			text : '刷新',
			iconCls : 'sy-refresh',
			handler : function(btn) {
				view_ds.removeAll();
				view_ds.load({
					params : {
						'fbh' : fbh_table
					}
				});
			}
		});
		
	var btn_save_parameter = new Ext.Button({
		text : '保存',
		iconCls : 'sy-sure',
		handler : function() {
			var view_record = view_grid.getSelectionModel().getSelected();
			if (view_record) {
				var array = view_sm.getSelections();
				
				var idData = ""; 
				var nameData = "";
				var descData = "";
				var typeData = "";
				
				Ext.each(array, function(view_record) {
					idData += view_record.get("fid") + ",";
					nameData += view_record.get("fname") + ",";
					descData += view_record.get("fdesc") + ",";
					typeData += view_record.get("ftype") + ",";
				});
				
				if (idData.length == 0) {
					idData = ""; 
					nameData = "";
					descData = "";
					typeData = "";
				} else {
					idData = idData.substring(0,
							idData.length - 1);
					nameData = nameData.substring(0,
							nameData.length - 1);
					descData = descData.substring(0,
							descData.length - 1);
					typeData = typeData.substring(0,
							typeData.length - 1);
				}

				//提交
				Ext.Ajax.request({
					url : 'reportInfo!saveReportParameters.do',// ?id='+uid,
					method : 'POST',
					params : {
						"idData" : idData,
						"nameData" : nameData,
						"descData" : descData,
						"typeData" : typeData,
						"fbh" : Ext.get("fbh_id").dom.value
					},
					success : function(response, action) {
						//Ext.MessageBox.alert('提示', '操作成功！');
						view_ds.removeAll();
						view_ds.reload();
					},
					// 提交失败的回调函数
					failure : function() {
						Ext.Msg.alert('错误', '服务器出现错误请稍后再试！');
					}
				});

			} else {
				Ext.MessageBox.alert('提示', '请选择一行或多行纪录进行操作！');
				return;
			}
		}
	});
	
	//生成表格
	var view_grid = new Ext.grid.EditorGridPanel({
		//closable : false,
		//region : 'center',
		height : 400,
		id : 'view_grid',
		layout : 'fit',
		width : 347,
		cm : view_colM,
		ds : view_ds,
		sm : view_sm,
		stripeRows : true,
		clicksToEdit:1,
		//defaults:{margins:'20 20 20 20'},
		enableHdMenu : false,
		tbar:[ '<b>参数列表:</b>', btn_add_parameter, '-', btn_save_parameter, '-', btn_delete_parameter, '-', btn_refresh_parameter ]
	});
	// 单元格编辑后事件处理 
    view_grid.on("afteredit", afterEdit, view_grid); 
    // 事件处理函数 
    function afterEdit(e) { 
        
    }; 
		
    

		var data_form_send = new Ext.FormPanel(
				{
					region : 'center',
					labelWidth : 55,
					frame : true,
					labelAlign : 'right',
					autoScroll : true,
					border : false,

					items : [ {
							columnWidth : .98,
							layout : "form",
							baseCls : "x-plain",
							labelWidth : 120,
							bodyStyle : 'padding-top:5px;',
							frame : true,
							items : [new Ext.form.RadioGroup({
			                       id:'ccgc_radio_id',
								   style:'width:100px; display: block;text-align:left;line-height:22px;',
								   height:20,
								   width:280,
			       				   fieldLabel : '调用存储过程',
			                       columns : 4,
			                       vertical :true,
			                       items:[
			                              {boxLabel:'关闭',inputValue:'1',name:'fprocedure_open',checked: true},
			                              {boxLabel:'取数',inputValue:'2',name:'fprocedure_open'},
			                              {boxLabel:'非取数',inputValue:'3',name:'fprocedure_open'}],
							        listeners : {
							        	'change' : function(rg, checked){
							        		//sqlContent.getEl().dom.readOnly = checked.getGroupValue() == 'yes' ? false : true;
									}}
						    }), {
								xtype : 'textarea',
								fieldLabel : '调用存储过程语句',
								id : 'fprocedure_name_id',
								name : 'fprocedure_name',
								readOnly : false,
								allowBlank : true,
								height : 40,
								width : 460
							}, {
								xtype : 'textarea',
								fieldLabel : 'SQL取数语句',
								id : 'fsql_id',
								name : 'fsql',
								readOnly : false,
								allowBlank : true,
								height : 140,
								width : 460
							}, {
								html : '　　　　　　例：select fmrdid, fname, fooffi where fmrdid=:fmrdid',
								height : 20,
								width : 460
							}, {
								xtype : 'hidden',
								fieldLabel : '报表编号',
								id : 'fbh_id_form',
								name : 'fbh'
							} ]
						} ]
				});

		var btn_save_form = new Ext.Button({
			text : '保存',
			iconCls : 'sy-sure',
			handler : function(btn) {
				var frm = data_form_send.getForm();
				if (frm.isValid()) {
					frm.submit({
						url : 'reportInfo!updateReportInfoSQL.do',
						method : 'POST',
						waitTitle : '请稍候',
						waitMsg : '正在提交表单数据,请稍候...',
						success : function(form, action) {
							Ext.MessageBox.alert('提示', '保存成功！');
							tree_op.root.reload();
						},
						failure : function(form, action) {
							Ext.Msg.show({
								title : '错误提示',
								msg : action.result.message,
								buttons : Ext.Msg.OK,
								fn : function() {
									unfield.focus(true);
									btn.enable();
								},
								icon : Ext.Msg.ERROR
							});
						},// failure
						icon : Ext.Msg.ERROR
					});
				}
				;// isValid
			}
		});

		var btn_reset_form = new Ext.Button({
			text : '清空',
			iconCls : 'sy-cancel',
			handler : function(btn) {
				
			}

		});

    
		/**********************************************************************/
	    var panelmenu2 = new Ext.Panel({
			region: "north",
			split:true,
	        minSize: 100,
	        maxSize: 600,
	        height : 290,
	        collapsible: false,
	        collapsed: false,
	        margins:'0 0 0 0',
	        layout:'fit',
	        layoutConfig:{
	            animate:true
	        },
	        tbar:[ btn_save_form, '-', btn_reset_form ],
			items :[data_form_send]
	   });	
	    
	    var panelmenu3 = new Ext.Panel({
			region: "center",
			split:true,
	        minSize: 100,
	        maxSize: 400,
	        collapsible: false,
	        collapsed: false,
	        margins:'0 0 0 0',
	        layout:'fit',
	        layoutConfig:{
	            animate:true
	        },
			items :[view_grid]
	   });	

	    var panel = new Ext.Panel({
	    	region: "center",
			border : true,
			layout : "border",
			title : '报表取数设置',
			margins:'0 5 0 0',
			items : [panelmenu2, panelmenu3]
		});
	    
				
				///////////////////////////////////  列表结束  ///////////////////////////////////////////////////////
				var viewport = new Ext.Viewport({
					layout : 'border',
					items    : [tree_nav, panel ]
				});
			});
</script>


</html>